<!DOCTYPE html>
<html>

<head>
    <title>比赛须知 - 答题系统</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --primary-color: #2196F3;
            --secondary-color: #1976D2;
            --background: #f8f9fa;
            --surface: #ffffff;
            --text-primary: #212121;
            --text-secondary: #757575;
            --error-color: #d32f2f;
            --header-height: 10rem;
            --desktop-side-margin: 2rem;
            --mobile-side-margin: 1rem;
            --button-height: 4rem;
            --spacing-xs: 0.5rem;
            --spacing-sm: 1rem;
            --spacing-md: 2rem;
            --spacing-lg: 3rem;
            --radius-base: 0.5rem;
            --radius-lg: 1.5rem;
            --shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.05);
            --shadow-md: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        html {
            font-size: 16px;
            -webkit-text-size-adjust: 100%;
        }

        body {
            width: 100%;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
            background: var(--background);
            font-family: 'Roboto', sans-serif;
            line-height: 1.5;
            color: var(--text-primary);
        }

        .container {
            width: 100%;
            padding-top: var(--header-height);
        }

        .header {
            position: fixed;
            top: 0;
            width: 100%;
            height: var(--header-height);
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: var(--shadow-md);
            z-index: 100;
        }

        .quiz-title {
            font-size: 2.5rem;
            font-weight: 500;
            max-width: 80%;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .notice-content {
            width: calc(100% - 2 * var(--desktop-side-margin));
            margin: var(--spacing-lg) auto;
            max-width: 80rem;
        }

        .notice-text {
            width: 100%;
            padding: var(--spacing-lg);
            background: var(--surface);
            border: 1px solid rgba(0, 0, 0, 0.1);
            border-radius: var(--radius-base);
            box-shadow: var(--shadow-sm);
            color: var(--text-secondary);
            font-size: 1.1rem;
            line-height: 1.7;
        }

        @media (min-width: 769px) {
            .notice-text {
                height: calc(100vh - var(--header-height) - 8rem);
                overflow-y: auto;
            }
        }

        @media (max-width: 768px) {
            .notice-content {
                width: calc(100% - 2 * var(--mobile-side-margin));
                margin: var(--spacing-md) auto;
            }

            .notice-text {
                height: auto;
                min-height: 60vh;
                padding-bottom: var(--button-height);
            }

            #buttonContainer {
                position: fixed;
                bottom: 0;
                /* 默认显示按钮 */
                left: 0;
                right: 0;
                background: var(--surface);
                box-shadow: 0 -4px 12px rgba(0, 0, 0, 0.1);
                padding: var(--spacing-sm);
                transition: bottom 0.3s ease;
            }

            .show-button #buttonContainer {
                bottom: 0;
            }
        }

        .action-button {
            padding: 1.2rem 3rem;
            font-size: 1.3rem;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            border: none;
            border-radius: 50px;
            color: white;
            cursor: pointer;
            transition: transform 0.2s;
            margin: 2rem auto;
            display: block;
        }

        .action-button:hover {
            transform: scale(1.05);
        }

        @media (max-width: 480px) {
            :root {
                --header-height: 8rem;
            }

            .quiz-title {
                font-size: 1.8rem;
            }

            .action-button {
                font-size: 1.1rem;
            }
        }
    </style>
    <script src="/basic.js"></script>
</head>

<body>
    <div class="container">
        <div class="loading-overlay" id="loading" style="display: none;">
            <div class="loading-spinner"></div>
        </div>

        <div class="header">
            <h1 class="quiz-title" id="quizTitle">比赛须知确认</h1>
        </div>

        <div class="notice-content">
            <div class="notice-text" id="quizNotice">正在加载比赛须知...</div>
        </div>

        <div id="buttonContainer">
            <button class="action-button" id="confirmBtn" disabled>📖 已阅读，进入比赛</button>
            <div class="error-message" id="errorMessage"></div>
        </div>
    </div>

    <script>
        const urlParams = new URLSearchParams(window.location.search);
        const quizId = urlParams.get('id');
        const loadingOverlay = document.getElementById('loading');
        const confirmBtn = document.getElementById('confirmBtn');
        const errorMessage = document.getElementById('errorMessage');

        async function verifyAccess() {
            try {
                showLoading();
                var response = await fetch('/quiz/verify/enter', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ quizId })
                });

                if (!response.ok) {
                    window.location.href = `enter?id=${quizId}`;
                    return;
                }

                response = await fetch(`/quiz/${quizId}`);
                const { data } = await response.json();
                document.getElementById('quizTitle').textContent = data.name;
                document.title = data.name;
                document.getElementById('quizNotice').textContent = data.notice || "暂无特别注意事项";
                confirmBtn.disabled = false;
            } catch (error) {
                console.error(error);
                errorMessage.textContent = '验证失败，请重新尝试';
                setTimeout(() => window.location.href = `enter?id=${quizId}`, 2000);
            } finally {
                hideLoading();
            }
        }

        function showLoading() {
            loadingOverlay.style.display = 'flex';
        }

        function hideLoading() {
            loadingOverlay.style.display = 'none';
        }

        function startQuiz() {
            confirmBtn.disabled = true;
            window.location.href = `quiz?id=${quizId}`;
        }

        // 初始化
        document.addEventListener('DOMContentLoaded', verifyAccess);
        confirmBtn.addEventListener('click', startQuiz);

        // 添加滚动监听启用按钮
        let lastScrollPos = 0;
        const noticeContent = document.querySelector('.notice-content');
        noticeContent.addEventListener('scroll', () => {
            const currentScroll = noticeContent.scrollTop + noticeContent.clientHeight;
            const shouldEnable = currentScroll >= noticeContent.scrollHeight - 50;

            if (shouldEnable && lastScrollPos < currentScroll) {
                confirmBtn.disabled = false;
                confirmBtn.innerHTML = '🎯 已阅读，进入比赛';
            }
            lastScrollPos = currentScroll;
        });
    </script>
</body>

</html>